﻿/*
 * AddRoom.java
 *
 * Created on __DATE__, __TIME__
 */

package scrn;


import java.awt.CardLayout;
import java.awt.Container;
import java.awt.image.BufferedImage;
import java.net.URL;

import javax.swing.DefaultListModel;
import javax.swing.SwingUtilities;

import cont.*;

import core.*;

import media.*;


/**
 * 
 * @author __USER__
 */
public class AddRoom extends javax.swing.JFrame {

private static final long serialVersionUID = 1L;

DefaultListModel listModel;


/** Creates new form AddRoom */
public AddRoom() {

	BufferedImage img = Pic.loadImage("icon.png");

	if (img != null) {
		this.setIconImage(img);
		img = null;
	}


	initComponents();
	try {
		Man.ioMan.outFindRoom(""/* здесь может быть маска поиска */,
				Setting.maxFind);
	} catch (Exception e) {
		Mess.err("AddRoom.commandAction", "Не удалось отправить запрос", e);
	}
}


/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

	panWait = new javax.swing.JPanel();
	textLoad = new javax.swing.JLabel();
	textWait = new javax.swing.JLabel();
	jProgressBar1 = new javax.swing.JProgressBar();
	butCancel = new javax.swing.JButton();
	panRooms = new javax.swing.JPanel();
	jScrollPane1 = new javax.swing.JScrollPane();
	listRooms = new javax.swing.JList();
	butAdd = new javax.swing.JButton();
	butCancel2 = new javax.swing.JButton();
	textHeader = new javax.swing.JLabel();

	setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
	setTitle("jChat.\u041a\u043e\u043c\u043d\u0430\u0442\u044b");
	addWindowListener(new java.awt.event.WindowAdapter() {

		public void windowClosing(java.awt.event.WindowEvent evt) {

			formWindowClosing(evt);
		}
	});
	getContentPane().setLayout(new java.awt.CardLayout());

	textLoad.setFont(new java.awt.Font("Tahoma", 1, 14));
	textLoad.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
	textLoad
			.setText("\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0441\u043f\u0438\u0441\u043a\u0430 \u043a\u043e\u043c\u043d\u0430\u0442");
	textLoad.setAlignmentX(0.5F);
	textLoad.setFocusable(false);
	textLoad.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

	textWait.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
	textWait
			.setText("\u041f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430, \u043f\u043e\u0434\u043e\u0436\u0434\u0438\u0442\u0435...");
	textWait.setAlignmentX(0.5F);
	textWait.setFocusable(false);
	textWait.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

	jProgressBar1.setFocusable(false);
	jProgressBar1.setIndeterminate(true);

	butCancel.setText("\u041e\u0442\u043c\u0435\u043d\u0430");
	butCancel.setAlignmentX(0.5F);
	butCancel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
	butCancel.addActionListener(new java.awt.event.ActionListener() {

		public void actionPerformed(java.awt.event.ActionEvent evt) {

			butCancelActionPerformed(evt);
		}
	});

	javax.swing.GroupLayout panWaitLayout = new javax.swing.GroupLayout(panWait);
	panWait.setLayout(panWaitLayout);
	panWaitLayout
			.setHorizontalGroup(panWaitLayout
					.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
					.addGroup(
							panWaitLayout
									.createSequentialGroup()
									.addGroup(
											panWaitLayout
													.createParallelGroup(
															javax.swing.GroupLayout.Alignment.LEADING)
													.addGroup(
															panWaitLayout
																	.createSequentialGroup()
																	.addContainerGap()
																	.addGroup(
																			panWaitLayout
																					.createParallelGroup(
																							javax.swing.GroupLayout.Alignment.LEADING)
																					.addComponent(
																							textLoad,
																							javax.swing.GroupLayout.DEFAULT_SIZE,
																							247, Short.MAX_VALUE)
																					.addComponent(
																							textWait,
																							javax.swing.GroupLayout.DEFAULT_SIZE,
																							247, Short.MAX_VALUE)
																					.addComponent(
																							jProgressBar1,
																							javax.swing.GroupLayout.DEFAULT_SIZE,
																							247, Short.MAX_VALUE)))
													.addGroup(
															panWaitLayout
																	.createSequentialGroup()
																	.addGap(93, 93, 93)
																	.addComponent(butCancel,
																			javax.swing.GroupLayout.DEFAULT_SIZE, 82,
																			Short.MAX_VALUE).addGap(82, 82, 82)))
									.addContainerGap()));
	panWaitLayout.setVerticalGroup(panWaitLayout.createParallelGroup(
			javax.swing.GroupLayout.Alignment.LEADING).addGroup(
			panWaitLayout
					.createSequentialGroup()
					.addGap(93, 93, 93)
					.addComponent(textLoad)
					.addGap(33, 33, 33)
					.addComponent(textWait)
					.addGap(57, 57, 57)
					.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE,
							javax.swing.GroupLayout.DEFAULT_SIZE,
							javax.swing.GroupLayout.PREFERRED_SIZE).addGap(78, 78, 78)
					.addComponent(butCancel).addGap(72, 72, 72)));

	getContentPane().add(panWait, "card1");

	panRooms.setName("");

	jScrollPane1.setViewportView(listRooms);

	butAdd
			.setText("<html><strong>\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c</strong></html>");
	butAdd.addActionListener(new java.awt.event.ActionListener() {

		public void actionPerformed(java.awt.event.ActionEvent evt) {

			butAddActionPerformed(evt);
		}
	});

	butCancel2.setText("\u041e\u0442\u043c\u0435\u043d\u0430");
	butCancel2.addActionListener(new java.awt.event.ActionListener() {

		public void actionPerformed(java.awt.event.ActionEvent evt) {

			butCancel2ActionPerformed(evt);
		}
	});

	textHeader.setFont(new java.awt.Font("Tahoma", 1, 12));
	textHeader.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
	textHeader
			.setText("\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u043c\u043d\u0430\u0442\u044b:");
	textHeader.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

	javax.swing.GroupLayout panRoomsLayout = new javax.swing.GroupLayout(panRooms);
	panRooms.setLayout(panRoomsLayout);
	panRoomsLayout.setHorizontalGroup(panRoomsLayout.createParallelGroup(
			javax.swing.GroupLayout.Alignment.LEADING).addGroup(
			panRoomsLayout
					.createSequentialGroup()
					.addContainerGap()
					.addGroup(
							panRoomsLayout
									.createParallelGroup(
											javax.swing.GroupLayout.Alignment.LEADING)
									.addComponent(jScrollPane1,
											javax.swing.GroupLayout.DEFAULT_SIZE, 247,
											Short.MAX_VALUE)
									.addComponent(textHeader,
											javax.swing.GroupLayout.DEFAULT_SIZE, 247,
											Short.MAX_VALUE)
									.addGroup(
											javax.swing.GroupLayout.Alignment.TRAILING,
											panRoomsLayout
													.createSequentialGroup()
													.addGap(16, 16, 16)
													.addComponent(butAdd,
															javax.swing.GroupLayout.DEFAULT_SIZE, 112,
															Short.MAX_VALUE).addGap(32, 32, 32)
													.addComponent(butCancel2).addGap(16, 16, 16)))
					.addContainerGap()));
	panRoomsLayout.setVerticalGroup(panRoomsLayout.createParallelGroup(
			javax.swing.GroupLayout.Alignment.LEADING).addGroup(
			javax.swing.GroupLayout.Alignment.TRAILING,
			panRoomsLayout
					.createSequentialGroup()
					.addGap(25, 25, 25)
					.addComponent(textHeader)
					.addGap(32, 32, 32)
					.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE,
							263, Short.MAX_VALUE)
					.addGap(28, 28, 28)
					.addGroup(
							panRoomsLayout
									.createParallelGroup(
											javax.swing.GroupLayout.Alignment.BASELINE)
									.addComponent(butAdd, javax.swing.GroupLayout.PREFERRED_SIZE,
											32, javax.swing.GroupLayout.PREFERRED_SIZE)
									.addComponent(butCancel2,
											javax.swing.GroupLayout.PREFERRED_SIZE, 32,
											javax.swing.GroupLayout.PREFERRED_SIZE))
					.addContainerGap()));


	panRoomsLayout.linkSize(javax.swing.SwingConstants.VERTICAL,
			new java.awt.Component[] { butAdd, butCancel2 });


	getContentPane().add(panRooms, "card2");

	pack();
}// </editor-fold>
//GEN-END:initComponents


private void formWindowClosing(java.awt.event.WindowEvent evt) {

	butCancelActionPerformed(null);
}


private void butAddActionPerformed(java.awt.event.ActionEvent evt) {

	int ind = listRooms.getSelectedIndex();
	try {
		Man.ioMan.outAddCont(Cont.ROOM, ind);
	} catch (Exception e) {
		Mess.err("AddRoom.commandAction", "Не удается отправить запрос");
	}
	butCancelActionPerformed(evt);
}


private void butCancel2ActionPerformed(java.awt.event.ActionEvent evt) {

	butCancelActionPerformed(evt);
}


private void butCancelActionPerformed(java.awt.event.ActionEvent evt) {

	Man.scrMan.addRoom = null;
	this.dispose();
}


/**
 * @param args
 *          the command line arguments
 */
public static void main(String args[]) {

	java.awt.EventQueue.invokeLater(new Runnable() {

		public void run() {

			new AddRoom().setVisible(true);
		}
	});
}


// =================================================== Добавить комнату в список
private void add(ContRoom room) {

	String text; // Имя и краткое описание

	URL url = Pic.getURL("room.png");

	//	text = "<html><img src='" + url + "' align='center' /> <strong>" + room.name
	//			+ "</strong><br />";
	//	if (room.info.descr != null) {
	//		text += room.info.descr;
	//	}
	//
	//	text += "</html>";

	text = "<html><img src='" + url + "' hspace='3' align='middle' /> <strong>"
			+ room.name + "</strong><br />";
	if (room.info.descr != null) {
		text += room.info.descr;
	}

	text += "</html>";

	listModel.addElement(text);
}


// ========================================== Получен результат поиска контактов
public void inFind(int contType, ContRoom[] conts) {

	Mess.m("AddRoom.inFind", "Отображаю список комнат");

	if (SwingUtilities.isEventDispatchThread()) {
		Mess.err("AddRoom.inFind", "Среда EDT!");
	} else {
		try {
			listModel = new DefaultListModel();
			for (int q = 0; q < conts.length; ++q) {
				add(conts[q]);
			}

			SwingUtilities.invokeAndWait(new Runnable() {

				public void run() {

					Container contentPane = getContentPane();
					CardLayout cardLayout = (CardLayout) (contentPane.getLayout());
					cardLayout.show(contentPane, "card2");

					listRooms.setModel(listModel);
				}
			});
		} catch (Exception e) {
			Mess.err("AddRoom.inFind", e);
		}
	}


}

//GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton butAdd;
private javax.swing.JButton butCancel;
private javax.swing.JButton butCancel2;
private javax.swing.JProgressBar jProgressBar1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JList listRooms;
private javax.swing.JPanel panRooms;
private javax.swing.JPanel panWait;
private javax.swing.JLabel textHeader;
private javax.swing.JLabel textLoad;
private javax.swing.JLabel textWait;
// End of variables declaration//GEN-END:variables

}